﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace zytech.common.baseform.sys
{
    public partial class PermissionView : Form
    {
        protected string SqlConnStr = "";
        protected int state = 0;
        public PermissionView()
        {
            InitializeComponent();
        }

        private void PermissionView_Load(object sender, EventArgs e)
        {
            //需要在继承的窗口中做的是向datagridview填充数据，加载treeview1的父节点，展开所有节点，加载第一个id的权限
        }

        #region 加载父节点
        protected virtual void InitialFirstNode()
        {
            string sql = "select * from sys_menu where ParentId = 0";
            DataTable dt = Functions.getDatatable(sql, SqlConnStr);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode node = new TreeNode();
                node.Tag = dr[0].ToString();        //节点的Tag设置为菜单id
                node.Text = dr[1].ToString();       //节点的Text设置为菜单名

                //加载子节点
                InitialSubNodeList(node);

                treeView1.Nodes.Add(node);
            }
        }
        #endregion

        #region 加载子节点
        protected virtual void InitialSubNodeList(TreeNode node)
        {
            string parentId = node.Tag.ToString();
            string sql = "select * from sys_menu where ParentId=" + parentId;
            DataTable dt = Functions.getDatatable(sql, SqlConnStr);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode subNode = new TreeNode();
                subNode.Tag = dr[0].ToString();
                subNode.Text = dr[1].ToString();

                InitialSubNodeList(subNode);

                node.Nodes.Add(subNode);
            }
        }
        #endregion

        #region 设置权限
        protected virtual void SetNodeChecked(TreeNode tv1, string str)
        {
            state = 0;
            string sql = "select abled from v_menu where cUserid = '" + str + "' and menu_id=" + tv1.Tag;
            SqlConnection conn = new SqlConnection(SqlConnStr);
            SqlCommand com3 = new SqlCommand(sql, conn);
            SqlDataAdapter da3 = new SqlDataAdapter(com3);
            DataSet ds3 = new DataSet();
            da3.Fill(ds3);
            foreach (DataRow row1 in ds3.Tables[0].Rows)
            {
                if (Convert.ToInt32(row1["abled"]) == 1)
                    state = 1;
            }
            if (state == 1)
                tv1.Checked = true;
            else
                tv1.Checked = false;
        }
        #endregion

        #region 加载权限
        protected virtual void setright(TreeNode tn, string str)
        {
            //使用递归加载权限
            SetNodeChecked(tn, str);
            foreach (TreeNode tr in tn.Nodes)
            {
                if (tn.Nodes != null && tn.Nodes.Count > 0)
                {
                    setright(tr, str);
                }
            }
        }
        #endregion

        /*#region 取消选择节点时,当前节点的父节点所有节点皆未被选中时，取消当前节点父节点的选中状态
        protected virtual void setParentNodeFalse(TreeNode currNode)
        {
            TreeNode parentNode = currNode.Parent;
            TreeNode curr = parentNode.FirstNode;
            while (curr != parentNode.LastNode)        //从同级的第一个节点循环到最后一个节点
            {
                if (curr.Checked == true)
                    break;
                else
                    curr = curr.NextNode;
            }
            if (curr == parentNode.LastNode && parentNode.LastNode.Checked == false)
                parentNode.Checked = false;     //如果与该节点同级的节点都未被选中则取消其父节点的选中状态
            //递归
            if (parentNode.Parent != null)
            {
                curr = parentNode.Parent.FirstNode;
                while (curr != parentNode.Parent.LastNode)
                {
                    if (curr.Checked == true)
                        break;
                    else
                        curr = curr.NextNode;
                }
                if (curr == parentNode.Parent.LastNode && parentNode.Parent.LastNode.Checked == false)
                    setParentNodeFalse(parentNode);
            }
        }
        #endregion

        #region 选中子节点同时选中父节点
        protected virtual void setParentNodeCheckedState(TreeNode currNode, bool state)
        {
            TreeNode parentNode = currNode.Parent;
            parentNode.Checked = state;
            if (currNode.Parent.Parent != null)
            {
                setParentNodeCheckedState(currNode.Parent, state);
            }
        }
        #endregion

        #region 选中节点为树节点则选中所有子节点
        protected virtual void setChildNodeCheckedState(TreeNode currNode, bool state)
        {
            TreeNodeCollection nodes = currNode.Nodes;
            if (nodes.Count > 0)
                foreach (TreeNode tn in nodes)
                {

                    tn.Checked = state;
                    setChildNodeCheckedState(tn, state);
                }
        }
        #endregion*/
        protected virtual void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            /*if (e.Action == TreeViewAction.ByMouse)
            {
                if (e.Node.Checked)
                {
                    if (e.Node.Parent == null)
                        setChildNodeCheckedState(e.Node, true);      //选中当前节点的同时选中其所有子节点
                    else
                    {
                        //选中当前节点的同时选中其父节点
                        setParentNodeCheckedState(e.Node, true);
                        //选中当前节点的同时选中其所有子节点
                        setChildNodeCheckedState(e.Node, true);
                    }
                }
                else
                {
                    //取消父节点选中状态之后，取消所有子节点的选中状态
                    setChildNodeCheckedState(e.Node, false);
                    //取消一个树节点的所有子节点选中状态后，取消此树节点的选中状态
                    if (e.Node.Parent != null)
                    {
                        setParentNodeFalse(e.Node);
                    }
                }
            }*/
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            /*if (e.ColumnIndex == 2)
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                {
                    e.Value = new string('*', e.Value.ToString().Length);
                }
            }
            for (int i = 0; i < dataGridView1.RowCount; i += 2)
            {
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Lavender;
            }*/
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //选择另一个人员时重新加载权限
             treeView1.Nodes.Clear();        //清除树
             InitialFirstNode();             //重新加载树
             treeView1.ExpandAll();          //展开所有节点
             for (int i = 0; i < treeView1.Nodes.Count; i++)
             {
                 treeView1.Nodes[i].Checked = false;
             }
             string id = dataGridView1.CurrentRow.Cells["cUserid"].Value.ToString();

             for (int i = 0; i < treeView1.Nodes.Count; i++)
             {
                 setright(treeView1.Nodes[0], id);
             }
        }
    }
}
